package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCommissionrecord;
import com.ruoyi.system.service.ISysCommissionrecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分佣金记录Controller
 *
 * @author ruoyi
 * @date 2021-06-13
 */
@RestController
@RequestMapping("/system/commissionrecord")
public class SysCommissionrecordController extends BaseController
{
    @Autowired
    private ISysCommissionrecordService sysCommissionrecordService;

    @Autowired
    private ISysDictDataService iSysDictDataService;

    /**
     * 查询积分佣金记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:commissionrecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCommissionrecord sysCommissionrecord)
    {
        startPage();
        List<SysCommissionrecord> list = sysCommissionrecordService.selectSysCommissionrecordList(sysCommissionrecord);
        return getDataTable(list);
    }

    /**
     * 导出积分佣金记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:commissionrecord:export')")
    @Log(title = "积分佣金记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysCommissionrecord sysCommissionrecord)
    {
        List<SysCommissionrecord> list = sysCommissionrecordService.selectSysCommissionrecordList(sysCommissionrecord);
        ExcelUtil<SysCommissionrecord> util = new ExcelUtil<SysCommissionrecord>(SysCommissionrecord.class);
        return util.exportExcel(list, "积分佣金记录数据");
    }

    /**
     * 获取积分佣金记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:commissionrecord:query')")
    @GetMapping(value = "/{commid}")
    public AjaxResult getInfo(@PathVariable("commid") Long commid)
    {
        return AjaxResult.success(sysCommissionrecordService.selectSysCommissionrecordById(commid));
    }

    /**
     * 新增积分佣金记录
     */
    @PreAuthorize("@ss.hasPermi('system:commissionrecord:add')")
    @Log(title = "积分佣金记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCommissionrecord sysCommissionrecord)
    {
        return toAjax(sysCommissionrecordService.insertSysCommissionrecord(sysCommissionrecord));
    }

    /**
     * 修改积分佣金记录
     */
    @PreAuthorize("@ss.hasPermi('system:commissionrecord:edit')")
    @Log(title = "积分佣金记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCommissionrecord sysCommissionrecord)
    {
        Long usernameintegral = sysCommissionrecord.getUsernameintegral();
        Long usernameupdintegral = sysCommissionrecord.getUsernameupdintegral();
        Long commid = sysCommissionrecord.getCommid();
        Long integrationLine =Long.parseLong(iSysDictDataService.selectDictValue("sys_integrationLine","积分线"));
        if(usernameintegral<integrationLine){
            AjaxResult ajaxResult = AjaxResult.error("总积分没有过提现值,不能提现");
            return ajaxResult;
        } else if(usernameintegral < usernameupdintegral){
            AjaxResult ajaxResult = AjaxResult.error("提现积分不能大于总积分");
            return ajaxResult;
        }
        SysCommissionrecord sysCommissionrecord1 = sysCommissionrecordService.selectSysCommissionrecordById(commid);
        sysCommissionrecord.setUsernameintegral(usernameintegral-usernameupdintegral);
        Long usernameupdintegral1 = sysCommissionrecord1.getUsernameupdintegral();
        if(usernameupdintegral1 == null){
            usernameupdintegral1 = 0L;
        }
        sysCommissionrecord.setUsernameupdintegral(usernameupdintegral1+usernameupdintegral);
        return toAjax(sysCommissionrecordService.updateSysCommissionrecord(sysCommissionrecord));
    }

    /**
     * 删除积分佣金记录
     */
    @PreAuthorize("@ss.hasPermi('system:commissionrecord:remove')")
    @Log(title = "积分佣金记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commids}")
    public AjaxResult remove(@PathVariable Long[] commids)
    {
        return toAjax(sysCommissionrecordService.deleteSysCommissionrecordByIds(commids));
    }
}
